arrayFromCompactFactory = $arrayFromCompactFactory; $this->nodeFactory = $nodeFactory; $this->nodeNameResolver = $nodeNameResolver; $this->nodeTypeResolver = $nodeTypeResolver; $this->templateGuesser = $templateGuesser; } public function create(?Return_ $return, DoctrineAnnotationTagValueNode $templateDoctrineAnnotationTagValueNode, ClassMethod $classMethod) : MethodCall { $renderArguments = $this->resolveRenderArguments($return, $templateDoctrineAnnotationTagValueNode, $classMethod); return $this->nodeFactory->createMethodCall('this', 'render', $renderArguments); } /** * @return Arg[] */ private function resolveRenderArguments(?Return_ $return, DoctrineAnnotationTagValueNode $templateDoctrineAnnotationTagValueNode, ClassMethod $classMethod) : array { $templateNameString = $this->resolveTemplateName($classMethod, $templateDoctrineAnnotationTagValueNode); $arguments = [$templateNameString]; $parametersExpr = $this->resolveParametersExpr($return, $templateDoctrineAnnotationTagValueNode); if ($parametersExpr instanceof Expr) { $arguments[] = new Arg($parametersExpr); } return $this->nodeFactory->createArgs($arguments); } private function resolveTemplateName(ClassMethod $classMethod, DoctrineAnnotationTagValueNode $templateDoctrineAnnotationTagValueNode) : string { $template = $this->resolveTemplate($templateDoctrineAnnotationTagValueNode); if (\is_string($template)) { return $template; } return $this->templateGuesser->resolveFromClassMethod($classMethod); } private function resolveParametersExpr(?Return_ $return, DoctrineAnnotationTagValueNode $templateDoctrineAnnotationTagValueNode) : ?Expr { $vars = []; $varsArrayItemNode = $templateDoctrineAnnotationTagValueNode->getValue('vars'); if ($varsArrayItemNode instanceof ArrayItemNode && $varsArrayItemNode->value instanceof CurlyListNode) { $vars = $varsArrayItemNode->value->getValues(); } if ($vars !== []) { return $this->createArrayFromArrayItemNodes($vars); } if (!$return instanceof Return_) { return null; } if ($return->expr instanceof Array_ && $return->expr->items !== []) { return $return->expr; } if ($return->expr instanceof MethodCall) { return $this->resolveMethodCall($return->expr); } if ($return->expr instanceof FuncCall && $this->nodeNameResolver->isName($return->expr, 'compact')) { $compactFunCall = $return->expr; return $this->arrayFromCompactFactory->createArrayFromCompactFuncCall($compactFunCall); } return null; } /** * @param ArrayItemNode[] $arrayItemNodes */ private function createArrayFromArrayItemNodes(array $arrayItemNodes) : Array_ { $arrayItems = []; foreach ($arrayItemNodes as $arrayItemNode) { $arrayItemNodeValue = $arrayItemNode->value; if ($arrayItemNodeValue instanceof StringNode) { $arrayItemNodeValue = $arrayItemNodeValue->value; } $arrayItems[] = new ArrayItem(new Variable($arrayItemNodeValue), new String_($arrayItemNodeValue)); } return new Array_($arrayItems); } private function resolveMethodCall(MethodCall $methodCall) : ?Expr { $returnStaticType = $this->nodeTypeResolver->getType($methodCall); if ($returnStaticType instanceof ArrayType) { return $methodCall; } return null; } private function resolveTemplate(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode) : ?string { $templateParameter = $doctrineAnnotationTagValueNode->getValue('template'); if ($templateParameter instanceof ArrayItemNode) { $templateParameterValue = $templateParameter->value; if ($templateParameterValue instanceof StringNode) { $templateParameterValue = $templateParameterValue->value; } if (\is_string($templateParameterValue)) { return $templateParameterValue; } } $arrayItemNode = $doctrineAnnotationTagValueNode->getSilentValue(); if ($arrayItemNode instanceof ArrayItemNode) { $arrayItemNodeValue = $arrayItemNode->value; if ($arrayItemNodeValue instanceof StringNode) { $arrayItemNodeValue = $arrayItemNodeValue->value; } if (\is_string($arrayItemNodeValue)) { return $arrayItemNodeValue; } } return null; } }